package spring.boot.admin.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import spring.boot.admin.mapper.SysMenuMapper;
import spring.boot.admin.model.SysMenu;

import java.util.List;

/**
 * 如果整个service都是使用的master数据源，可以放在类上面
 */
@Service
@DS("master")
public class SysMenuService extends ServiceImpl<SysMenuMapper, SysMenu> {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 根据父id查询菜单列表 倒序
     */
    private List<SysMenu> selectByPid(Long pid) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", pid);
        wrapper.orderByDesc("sort");
        return list(wrapper);
    }


    /**
     * 查询主页左侧菜单 tree
     */
    public List<SysMenu> selectIndexLeftList() {
        List<SysMenu> list = selectByPid(0L);
        for (int i = 0; i < list.size(); i++) {
            List<SysMenu> listTwo = selectByPid(list.get(i).getId());
            list.get(i).setChildren(listTwo);
        }
        return list;
    }

    /**
     * 菜单管理  列表查询
     */
    public List<SysMenu> selectList(Long pid) {
        List<SysMenu> pList = selectByPid(pid);
        if (pList != null && pList.size() > 0) {
            for (int i = 0; i < pList.size(); i++) {
                //security 方法级权限 显示时处理前缀
                if (pList.get(i).getAuthority().startsWith("ROLE_")) {
                    pList.get(i).setAuthority(pList.get(i).getAuthority().substring(5));
                }
                List<SysMenu> childrenList = selectByPid(pList.get(i).getId());
                //判断tree是否是可展开状态
                if (childrenList != null && childrenList.size() > 0) {
                    pList.get(i).setState("closed");
                } else {
                    pList.get(i).setState("open");
                }
            }
        }
        return pList;
    }


    /**
     * 新增菜单
     */
    public String insert(Long pid, String name, String url, String authority, Integer enable, Integer sort) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setPid(pid);
        sysMenu.setName(name);
        sysMenu.setUrl(url);
        sysMenu.setAuthority("ROLE_" + authority);
        sysMenu.setEnable(enable);
        sysMenu.setSort(sort);
        sysMenu.setCreateTime(String.valueOf(System.currentTimeMillis()));
        save(sysMenu);
        return "1";
    }


    /**
     * 删除菜单，删除前要判断是否有角色关联，是否有子菜单
     */
    public String deleteById(Long id) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("pid", id);
        int count = count(wrapper);
        if (count > 0) {
            return "2";
        } else {
            int roleMenuNum = sysRoleMenuService.countByMenuId(id);
            if (roleMenuNum > 0) {
                return "3";
            } else {
                removeById(id);
                return "1";
            }
        }
    }


    /**
     * 根据主键id更新数据
     */
    public String updateMenuById(Long id, String name, String url, String authority, Integer sort, Integer enable) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setName(name);
        sysMenu.setUrl(url);
        sysMenu.setAuthority("ROLE_" + authority);
        sysMenu.setSort(sort);
        sysMenu.setEnable(enable);
        updateById(sysMenu);
        return "1";
    }

    /**
     * 根据父id，查询所有子菜单的 id和name
     */
    public List<SysMenu> selectSimpleMenuByPid(Long pid) {
        return baseMapper.selectSimpleMenuByPid(pid);
    }
}
